<?php

namespace App\Http\Controllers\Back;

use App\Http\Controllers\Controller;
use App\Model\VipFeeConfig;
use Illuminate\Http\Request;

class VipProductController extends Controller
{
    public function index(VipFeeConfig $vipFeeConfig)
    {
        $product = $vipFeeConfig->get();
        return $this->renderJson($product);
    }

    public function show($id, VipFeeConfig $vipFeeConfig)
    {
        return $this->renderJson($vipFeeConfig->findOrFail($id));
    }

    public function store(Request $request, VipFeeConfig $vipFeeConfig)
    {
        $this->validate($request, [
            'name' => 'required',
            'days' => 'required|numeric',
            'money' => 'required|numeric',
            'auto_charge' => 'boolean',
            'type' => 'in:1,2,0'
        ], [
            'required' => ':attribute不能为空',
            'numeric' => '必须为数字',
            'boolean' => '必须为布尔类型或者01'
        ] ,[
            'name' => '产品名称',
            'days' => '天数',
            'money' => '价格',
            'auto_charge' => '自动续费'
        ]);

        $vipFeeConfig->create($request->all());
        return $this->success();
    }

    public function update(Request $request, $id, VipFeeConfig $vipFeeConfig)
    {
        $this->validate($request, [
            'name' => 'required',
            'days' => 'required|numeric',
            'money' => 'required|numeric',
            'auto_charge' => 'boolean'
        ], [
            'required' => ':attribute不能为空',
            'numeric' => '必须为数字',
            'boolean' => '必须为布尔类型或者01'
        ] ,[
            'name' => '产品名称',
            'days' => '天数',
            'money' => '价格',
            'auto_charge' => '自动续费'
        ]);

        $m = $vipFeeConfig->findOrFail($id);

        $m->update($request->all());
        return $this->success();
    }

    public function destroy($id, VipFeeConfig $vipFeeConfig)
    {
        try {
            $vipFeeConfig->findOrFail($id)->delete();
        } catch (\Exception $e) {
            return $this->error('删除失败');
        }
        return $this->success();
    }
}
